﻿using System;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.IO;

namespace SalaryChannel.Web
{
    [ServiceContract(Namespace = "http://salary.yangchao.ts.gs.chinamobile/")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class ReportService : ServiceAuthRoot
    {
        [OperationContract]
        public List<string> GetValidReport(int TableType)
        {
            try
            {
                // 在此处添加操作实现
                using (In_DataClassesDataContext context = new In_DataClassesDataContext())
                {
                    var query = from q in context.ReportList
                                where q.IsValid == 1 && q.ReportType ==TableType
                                orderby q.ID
                                select new { report = string.Format("[{0}]{1}", q.ID, q.Name) };
                    List<string> reports = new List<string>(query.Count());
                    foreach (var r in query)
                    {
                        reports.Add(r.report);
                    }
                    return reports;
                }
            }
            catch (Exception ex)
            {
                LogManager.Write("ReportService.GetValidReport:" + ex.Message);
                return new List<string>();
            }
        }

        // 在此处添加更多操作并使用 [OperationContract] 标记它们

        [OperationContract]
        public XDataTable GetReportByID(int ReportID, string ChannelID, string AreaID,string Time)
        {
            XDataTable xtable = null;

            try
            {

                xtable = new XDataTable(GetReportByIDIntoTable(ReportID,ChannelID,AreaID,Time,100));

            }
            catch (Exception ex)
            {
                
                LogManager.Write("ReportService.GetReportByID:" + ex.Message);
                return null;
            }
            return xtable;
        }

        private System.Data.DataTable GetReportByIDIntoTable(int ReportID, string ChannelID, string AreaID, string Time,int RowCount)
        {
            System.Data.DataTable table = new System.Data.DataTable("Report"+ReportID.ToString());
              using (SqlConnection con = new SqlConnection(SqlString))
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.Connection = con;
                    cmd.CommandType = System.Data.CommandType.StoredProcedure;
                    cmd.CommandText = "SP_GetReportByID";

                    cmd.Parameters.AddRange(new SqlParameter[] { 
                    new SqlParameter("@ReportID",ReportID),
                    new SqlParameter("@ChannelID",ChannelID),
                    new SqlParameter("@AreaID",AreaID),
                    new SqlParameter("@Time",Time),   
                    new SqlParameter("@RowCount",RowCount) 
                });
                    SqlDataAdapter sda = new SqlDataAdapter(cmd);
                    sda.Fill(table);
                }

              return table;
        }

        [OperationContract]
        public string GetReportByIDIntoFile(int ReportID, string ChannelID, string AreaID, string Time)
        {
            try
            {
                Authoricate();
                System.Data.DataTable table = GetReportByIDIntoTable(ReportID, ChannelID, AreaID, Time, 0);

                string filename = WriteTableIntoFile(table, "Report"+ReportID.ToString());
                return filename;

            }
            catch (Exception ex)
            {

                LogManager.Write("InforService.ExportInfoIntoFile:" + ex.Message);
                return "-1";
            }
        }


        [OperationContract]
        public List<string> GetArea(string CityCode)
        {
            try
            {
                using (DataClassesQueryDataContext context = new DataClassesQueryDataContext())
                {
                    var query = from q in context.CM_Region
                                where q.CityCode == CityCode
                                orderby q.CountyCode
                                select new { area = string.Format("[{0}]{1}", q.CountyCode, q.CountyName) };
                    List<string> areas = new List<string>(query.Count());
                    foreach (var r in query)
                    {
                        areas.Add(r.area);
                    }
                    return areas;
                }

            }
            catch (Exception ex)
            {
                LogManager.Write("ReportService.GetArea:" + ex.Message);
                return null;
            }
        }
    }
}
